package com.dcy.gateway.satoken;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.collection.CollUtil;
import com.dcy.common.constant.Constant;
import com.dcy.system.api.api.AUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author：dcy
 * @Description:
 * @Date: 2021/4/13 15:00
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @DubboReference(version = "1.0.0", check = false)
    private AUserService aUserService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<String> getPermissionList(Object loginId, String loginKey) {
        Set<String> resource;
        Object o = redisTemplate.opsForValue().get(Constant.REDIS_RESOURCE_CODE + loginId);
        if (o != null) {
            resource = (Set<String>) o;
        } else {
            resource = aUserService.getResourcesCodeByUserId((String) loginId);
        }
        return CollUtil.newArrayList(resource);
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginKey) {
        Set<String> roleKeys;
        Object o = redisTemplate.opsForValue().get(Constant.REDIS_ROLE_KEY + loginId);
        if (o != null) {
            roleKeys = (Set<String>) o;
        } else {
            roleKeys = aUserService.getAuthRoleKeyByUserId((String) loginId);
        }
        return CollUtil.newArrayList(roleKeys);
    }
}
